package com.modules.controller.sys;


import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.common.controller.MyController;
import com.common.log.LogData;
import com.common.vo.Result;
import com.modules.entity.sys.RoleEntity;
import com.modules.service.sys.RoleService;

import cn.hutool.core.util.StrUtil;


/**
 * role控制层
 * @author yyq
 *
 */
@Controller
@RequestMapping("/sys/role")
public class RoleController extends MyController<RoleEntity>{
	
	@Autowired
	private RoleService service;
	
	
	/**
	 * 查询
	 * @return
	 */
	@RequestMapping("/select")
	@ResponseBody
	public Result<?> select(RoleEntity role){
		LambdaQueryWrapper<RoleEntity> queryWrapper = new LambdaQueryWrapper<RoleEntity>();
		if(StrUtil.isNotBlank(role.getRname())) {
			queryWrapper.like(RoleEntity::getRname, role.getRname());
		}
		if(StrUtil.isNotBlank(role.getType())) {
			queryWrapper.eq(RoleEntity::getType, role.getType());
		}
		if(role.getState() != null) {
			queryWrapper.eq(RoleEntity::getState, role.getState());
		}
		queryWrapper.ne(RoleEntity::getRid, 1);
		IPage<RoleEntity> iPage = service.page(getPage(), queryWrapper);
		return Result.success(iPage.getRecords(), iPage.getTotal());
	}
	
	/**
	 * 查询所有
	 * @return
	 */
	@RequestMapping("/selectAll")
	@ResponseBody
	public Result<?> selectAll(RoleEntity role){
		List<RoleEntity> list = service.list(new LambdaQueryWrapper<RoleEntity>());
		return Result.success(list);
	}

	/**
	 * 保存
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public Result<?> save(RoleEntity role, HttpServletRequest request){
		role.setOid(1); // 在管理多个项目组织时使用
		if (StrUtil.isEmpty(role.getRname())) {
            return Result.fail("参数错误");
        }
		service.saveOrUpdate(role);
		return Result.success();
	}
	
	/**
	 * 删除
	 * @return
	 */
	@LogData(module = "角色管理", remarks = "删除单个")
	@RequestMapping("/delete")
	@ResponseBody
	public Result<?> delete(RoleEntity role, HttpServletRequest request){
		return service.delete(role.getRid());
	}
	
	/**
	 * 修改状态
	 * @return
	 */
	@LogData(module = "角色管理", remarks = "修改状态")
	@RequestMapping("/updateState")
	@ResponseBody
	public Result<?> updateState(RoleEntity role){
		service.updateById(role);
		return Result.success();
	}
	
	/**
	 * 批量删除
	 * @return
	 */
	@LogData(module = "角色管理", remarks = "批量删除")
	@RequestMapping("/deleteBatch")
	@ResponseBody
	public Result<?> deleteBatch(String ids){
		service.removeByIds(Arrays.asList(ids.split(",")));
		return Result.success();
	}
	
	/**
	 * 查询授权菜单
	 * @return
	 */
	@RequestMapping("/selectRoleMenuTree")
	@ResponseBody
	public Result<?> selectRoleMenuTree(Integer rid){
		return service.selectRoleMenuTree(rid);
	}
	
	/**
	 * 授权菜单
	 * @return
	 */
	@LogData(module = "角色管理", remarks = "授权菜单")
	@RequestMapping("/saveRoleMenu")
	@ResponseBody
	public Result<?> menuPows(Integer rid, Integer[] mids){
		return service.saveRoleMenu(rid, mids);
	}


}